#include "main.h"


int sub_flag = 0;
int pub_flag = 0;
int ble_conn_flag = 0;


static void my_message_callback(struct mosquitto *mosq, void *obj, const struct mosquitto_message *msg);

void su_hander(int signo)
{
    sub_flag = 1;
}

void pu_hander(int signo)
{
    pub_flag = 1;
}



void *thread_worker1_ble_wr(void *args)
{
    int                   temp;
    char                 *su_buf;
    const char           *wr_uuid = "c304";
    char                  uuid_str[MAX_LEN_UUID_STR + 1];   //MAX_LEN_UUID_STR =37
    static uuid_t         uuid;   //be used to change type :wrtite_uuid(char) to uuid(uuid_t)

	 while( !ble_conn_flag);

    if (!conn)
    {
        log_error("error connection adderss\n");
    }


    signal(SIGUSR1, su_hander);


    memset(&uuid_str, 0, sizeof(uuid_str));
    strncpy(uuid_str, wr_uuid, sizeof(uuid_str));

    if (gattlib_string_to_uuid(uuid_str, strlen(uuid_str) + 1, &uuid) < 0)
    {
        log_error("String to uuid_t failed: %s\n", strerror(errno));
        return NULL;
    }

    while(1)
    {
        if (sub_flag)
        {
            sub_flag = 0;

            if (NULL == (su_buf = su_get_head()))
            {
                continue;
            }

            temp = gattlib_write_char_by_uuid(conn, &uuid, su_buf, strlen(su_buf)+1);
            if (temp != GATTLIB_SUCCESS)
            {
                if(temp == GATTLIB_NOT_FOUND)
                {
                    log_error("Could not find GATT Characteristic with UUID: %s", strerror(errno));
                    su_dequeue();
                    return NULL;
                }
                else
                {
                    log_error("Error while writing GATT Characteristic with UUID %s (ret:%d)\n",uuid_str, temp);
                    su_dequeue();
                    return NULL;
                }
            }
            su_dequeue();
        }

    }
    return NULL;
}



void *thread_worker2_mqtt(void *args)
{
    char                 *send_msg;
    int                   i;
    int                   mid, mid1;
    int                   retain = 0;
    struct mosquitto     *mosq;
    struct mqtt_conf_s   *conn_par = (mqtt_conf_t *)args;

    signal(SIGUSR2, pu_hander);

    if (!args)
    {
        log_error("connect par is null");
        return NULL;
    }


    mosq = mqtt_init(conn_par);
    if (mosq == NULL)
    {
        goto cleanup;
    }

    //sub  messages callback
    mosquitto_message_callback_set(mosq, my_message_callback);

    if (mqtt_connect(mosq, conn_par) < 0)
    {
        goto cleanup;
    }
    log_info("mqtt connect successfully!");

    if (mosquitto_subscribe(mosq, &mid, conn_par->subtopic, conn_par->qos) != MOSQ_ERR_SUCCESS)
    {
        log_error("mosquitto subsribe failure: %s", strerror(errno));
    }

    while (1)
    {
        if (pub_flag)
        {
            pub_flag = 0;

            if (NULL == (send_msg = get_head()))
            {
                continue;
            }
            if (mosquitto_publish(mosq, &mid1, conn_par->pubtopic, strlen(send_msg)+1, send_msg, conn_par->qos, retain) != MOSQ_ERR_SUCCESS)
            {
                log_error("mosquitto publish data failure: %s", strerror(errno));
                continue;
            }

            //delete queue
            dequeue();
        }
    }

    cleanup:
    mqtt_clean(mosq);
    return NULL;
}


static void my_message_callback(struct mosquitto *mosq, void *obj, const struct mosquitto_message *msg)
{
    su_enqueue((char *)msg->payload);
    printf("%s\n", (char *)msg->payload);
    pthread_kill(ble_wr_tid, SIGUSR1);
}


